package com.iwhalecloud.bss.kite.cucc.service.attr.greenWayOrder.handlers;

import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.attr.IGreenWayOrderService;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalErrorConsts;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.manager.scene.service.QuerySceneInstService;
import com.ztesoft.bss.common.util.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class InternetGreenWayOrderServiceImpl implements IGreenWayOrderService {
    @Autowired
    private QuerySceneInstService querySceneInstService;

    @Override
    public void check(String sceneInstId, String custId) {
        KiteSceneInst kiteSceneInst = querySceneInstService.getSceneInst(sceneInstId);
        String rootInstId = kiteSceneInst.getRootInstId();
        //订单性质
        KiteAttr orderPropAttr = querySceneInstService.getCommonAttrsByFieldName(sceneInstId, "ORD10001", "93");
        //起租方式
        KiteAttr startRentAttr = querySceneInstService.getCommonAttrsByFieldName(sceneInstId, "ORD10002", "93");
        //选择绿色工单,那么起租方式只能为手工起租
        if (Objects.nonNull(orderPropAttr) && "5".equals(orderPropAttr.getValue())) {
            if (Objects.nonNull(startRentAttr) && !"0".equals(startRentAttr.getValue())) {
                LocalErrorConsts.BSS_KITE_ATTR_43085.throwOut();
            }
            KiteProdInst prodInst = querySceneInstService.listProdInstsFromOfferInst(custId, rootInstId).stream()
                .filter(inst -> !KiteStringUtils.equalsAny(inst.getActionType().name(), ActionType.X.name(), ActionType.D.name()))
                .findFirst().orElse(null);
            //跨域工单不允许选择绿色工单
            if (isMultiDomain(prodInst)) {
                LocalErrorConsts.BSS_KITE_ATTR_43086.throwOut();
            }
        }
    }

    @Override
    public boolean isMultiDomain(KiteProdInst prodInst) {
        if (Objects.nonNull(prodInst)) {
            //省
            KiteAttr provinceAttr = prodInst.getAttr("2_0000220");
            //上联机房
            KiteAttr topAlliedMachineRoomAttr = prodInst.getAttr("2_0000065");
            //若‘用户地址是否需要广播’
            KiteAttr userAddressBroadcastAttr = prodInst.getAttr("2_0000087");
            //路由协议为BGP
            KiteAttr RouteProtocolAttr = prodInst.getAttr("2_0000062");
            // 广播是否大于16C
            KiteAttr broadcastWith16CAttr = prodInst.getAttr("200002024");
            // IPV6地址
            KiteAttr IPV6AddressAttr = prodInst.getAttr("2_0000203");

            //获取业务受理工号归属
            String provinceNbr = ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
            // 判断是否省际
            if (!KiteStringUtils.isEqual(provinceAttr.getValue(), provinceNbr)) {
                return true;
            }
            if(KiteStringUtils.isEqual(topAlliedMachineRoomAttr.getValue(), "1_0000349")){
                return true;
            }
            if(KiteStringUtils.isEqual(userAddressBroadcastAttr.getValue(), "1_0000106")&& KiteStringUtils.isEqual(RouteProtocolAttr.getValue(), "1_0000213")){
                return true;
            }
            if(KiteStringUtils.isEqual(userAddressBroadcastAttr.getValue(), "1_0000106")&& KiteStringUtils.isEqual(RouteProtocolAttr.getValue(), "1_0000340")
               &&(KiteStringUtils.isEqual(broadcastWith16CAttr.getValue(), "1_0000106") || KiteStringUtils.isNotEmpty(IPV6AddressAttr.getValue()))){
                return true;
            }
        }
        return false;
    }
}
